package com.ra.controller;


import com.ra.pojo.RespBean;
import com.ra.pojo.RespPageBean;
import com.ra.pojo.dto.ArticleAdminDTO;
import com.ra.pojo.dto.ArticleDTO;
import com.ra.pojo.dto.ArticleSearchDTO;
import com.ra.pojo.dto.ConditionDTO;
import com.ra.service.IArticleService;
import com.ra.service.UploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 今晚几点睡_Ada
 * @since 2023-03-09
 */
@RestController
@Api(tags = "文章模块")
public class ArticleController {

    @Autowired
    private IArticleService articleService;

    //文件上传策略
    @Autowired
    private UploadService uploadService;

    //导入文件上下文
   // @Autowired
   // private ArticleImportStrategyContext articleImportStrategyContext;
   // articles/
    @ApiOperation("上传文章图片")  
    @PostMapping("admin/articles/images")
    @ApiImplicitParam(name = "file", value = "文章图片", required = true, dataType = "MultipartFile")
    public RespBean<String> saveArticleImages(MultipartFile file){
        return RespBean.success(uploadService.uploadFile(file,"articles/"));
    }

    @ApiOperation("保存或者修改文章")
    @PostMapping("/admin/articles")
    public RespBean<?> saveOrUpdateArticle(@Valid @RequestBody ArticleDTO articleDTO) {
        articleService.saveOrUpdateArticle(articleDTO);
        return RespBean.success();
    }
    //分页查询
    //https://admin.linhaojun.top/api/admin/categories/search

    @ApiOperation("分页查询所有文章")
    @GetMapping("/admin/articles")
    public RespBean<RespPageBean<ArticleAdminDTO>> listArticlesAdmin(ConditionDTO conditionDTO){
        return RespBean.success(articleService.listArticlesAdmin(conditionDTO));
    }

    @ApiOperation(value = "搜索文章")
    @GetMapping("/articles/search")
    public RespBean<List<ArticleSearchDTO>> listArticlesBySearch(ConditionDTO condition) {
        return RespBean.success(articleService.listArticlesBySearch(condition));
    }
















}

